﻿//-----------------------------------------------------------------------
// <copyright file="IsolatedStorageHandler.cs" company="FIT VUT">
//     Copyright (c) FIT VUT. All rights are reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BookCaching.Data.Storage
{
    public class IsolatedStorageHandler
    {
        #region Private fields
        private static IsolatedStorageHandler isolatedStorageHandlerInstance = new IsolatedStorageHandler();

        private IsolatedStorageSettings settings;

        private IsolatedStorageHandler()
        {
            this.settings = IsolatedStorageSettings.ApplicationSettings;
        }
        #endregion

        #region Public properties
        /// <summary>
        /// Gets the singleton's instance.
        /// </summary>
        public static IsolatedStorageHandler Instance
        {
            get
            {
                return isolatedStorageHandlerInstance;
            }
        }
        #endregion

        public bool AddOrUpdateValue(string key, object value)
        {
            bool valueChanged = false;

            if (this.settings.Contains(key))
            {
                this.settings[key] = value;
                valueChanged = true;
            }
            else
            {
                this.settings.Add(key, value);
                valueChanged = true;
            }

            this.settings.Save();

            return valueChanged;
        }

        public T GetValueOrDefault<T>(string key, T defaultValue)
        {
            try
            {
                T value;

                if (this.settings.Contains(key))
                {
                    value = (T)this.settings[key];
                }
                else
                {
                    value = defaultValue;
                }

                return value;
            }
            catch
            {
                return defaultValue;
            }
        }

        public void RemoveValue(string key)
        {
            if (this.settings.Contains(key))
            {
                this.settings.Remove(key);
            }

            this.settings.Save();
        }

        public void AddOrUpdateImage(string imageName, BitmapImage image)
        {
            IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication();
            if (isolatedStorageFile.FileExists(imageName))
            {
                isolatedStorageFile.DeleteFile(imageName);
            }

            IsolatedStorageFileStream fileStream = isolatedStorageFile.CreateFile(imageName + ".jpg");
            WriteableBitmap wb = new WriteableBitmap(image);

            Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
        }

        public BitmapImage GetImage(string imageName)
        {
            BitmapImage toReturn = new BitmapImage();
            IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication();
            if (!isolatedStorageFile.FileExists(imageName + ".jpg"))
            {
                return null;
            }

            IsolatedStorageFileStream fileStream = isolatedStorageFile.OpenFile(
                                                                                imageName + ".jpg", 
                                                                                System.IO.FileMode.Open, 
                                                                                System.IO.FileAccess.Read);
            toReturn.SetSource(fileStream);

            return toReturn;
        }
    }
}
